package com.huitone.gddw.service.impl.worktable;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.InvalidUse;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IInvalidUseDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IInvalidUseService;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.dto.InvalidUseDTO;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 无效用户 接口实现类
 * @author wuluodan
 *
 */
@Service
public class InvalidUseService implements IInvalidUseService {

	@Autowired
	private IInvalidUseDao invalidUseDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<InvalidUseDTO> selectPage(QueryPage<InvalidUse> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("invalidUser", page.getParams().get("invalidUser"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<InvalidUseDTO> result = invalidUseDao.selectPage(map);
		int totalCount = invalidUseDao.selectCount(map);
		
		QueryPage<InvalidUseDTO> queryPage = new QueryPage<InvalidUseDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public InvalidUse selectByPrimaryKey(String recId) {
		return invalidUseDao.selectByPrimaryKey(recId);
	}

	@Override
	public InvalidUseDTO toUpdate(String recId) {
		return new InvalidUseDTO(this.selectByPrimaryKey(recId));
	}

	@Override
	public void save(InvalidUse entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		invalidUseDao.insertSelective(entity);
		
		this.updateParamRec("添加无效用户");
	}

	@Override
	public void update(InvalidUse entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		invalidUseDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新无效用户");
	}

	@Override
	public void delete(String recId) {
		invalidUseDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除无效用户");
	}

	@Override
	public List<InvalidUseDTO> getRecord(Map<String, Object> params) {
		List<InvalidUseDTO> list = invalidUseDao.getRecord(params);
		return list;
	}

	@Override
	public int saveBatch(List<InvalidUse> list) {
		for (InvalidUse invalidUse : list) {
			invalidUse.setRecId(StringUtils.uuid());
			invalidUse.setInsertTime(new Date());
			invalidUse.setInsertStaffId(SysLoginUtil.getLoginUserId());
			invalidUse.setLastModTime(invalidUse.getInsertTime());
			invalidUse.setLastModStaffId(invalidUse.getInsertStaffId());
		}
		if (!list.isEmpty() && list.size() > 0) {
			this.updateParamRec("批量保存无效用户");
			return invalidUseDao.insertbatch(list);
		}
		return 0;
	}

	@Override
	public void delAllByFileIds(List<String> list) {
		if (!list.isEmpty() && list.size() > 0) {
			this.updateParamRec("根据多个文件ID删除无效用户");
			invalidUseDao.delAllByFileIds(list);
		}
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.INVALIDUSE, desc);
	}

	@Override
	public void changeStatusByFileIds(String[] fileIds, String status) {
		List<String> list = Arrays.asList(fileIds);
		if (!list.isEmpty() && list.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("status", status);
			map.put("list", list);
			invalidUseDao.changeStatus(map);
		}
	}

}
